const fs=require('fs')
const path=require('path')
const { params } = require('../router')
const dbpath=path.join('/Users/Allj/lljstudyl ','/userDb.json')
const addUserService=async (params)=>{
    let myData=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
    myData.push(params)
    await fs.writeFileSync(dbpath,JSON.stringify(myData),'utf-8')
    return true
}
const deleteUserService=async (params)=>{
    let myData=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
    let arr=[]
    await myData.forEach(item => {
        if (item.id!=params) {
            arr.push(item)
        }
    });
    await fs.writeFileSync(dbpath,JSON.stringify(arr),'utf-8')
    return true
}
const putUserService=async(params)=>{
    let myData=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
    let arr=[]
    await myData.forEach(item=>{
        if (item.id!=params) {
            arr.push(item)
        }
    })
    await fs.writeFileSync(dbpath,JSON.stringify(arr),'utf-8')
    return true
}
const selectUserService=async(params)=>{
    let myData=await JSON.parse(fs.readFileSync(dbpath,'utf-8'))
    let arr=[]
    await myData.forEach(item=>{
        if (item.id==params) {
            arr.push(item)
        }
    })
    await fs.writeFileSync(dbpath,JSON.stringify(arr),'utf-8')
    return true
}
module.exports={
    addUserService,
    deleteUserService,
    putUserService,
    selectUserService
}